////此页面为物流管理部分公用的函数

$(document).ready(function() {
	//	输入:必须在页面中设定 search_state
	initSearchData();

})
var SEARCH_INTERNAL_NAME = 1; //内部商品名称
var SEARCH_ORIGINAL_NAME = 2; //原始产品名称
var SEARCH_CUSTOMER_TYPE = 4; //用户组
var SEARCH_CUSTOMER_NAME = 8; //配送单位
var SEARCH_PROVIDERTYPE = 16; //供应商类型
var SEARCH_PROVIDER_NAME = 32; //供应商名称
var SEARCH_DISTRIBUTION_DATE = 64; //配送时间

function buildSearchOptions(searchType, newdata) {
	var data = [{
		"name": "内部商品名称",
		"type": "INTERNAL_NAME",
		"className": "easyui-combobox",
		"id": "input_INTERNAL_NAME"
	}, {
		"name": "原始产品名称",
		"type": "ORIGINAL_NAME",
		"className": "easyui-textbox",
		"id": "input_ORIGINAL_NAME"
	}, {
		"name": "用户组",
		"type": "CUSTOMER_TYPE",
		"className": "easyui-combobox",
		"id": "input_CUSTOMER_TYPE"
	}, {
		"name": "配送单位",
		"type": "CUSTOMER_NAME",
		"className": "easyui-combobox",
		"id": "input_CUSTOMER_NAME"
	}, {
		"name": "供应商类型",
		"type": "PROVIDERTYPE",
		"className": "easyui-combobox",
		"id": "input_PROVIDERTYPE"
	}, {
		"name": "供应商名称",
		"type": "PROVIDER_NAME",
		"className": "easyui-combobox",
		"id": "input_PROVIDER_NAME"
	}, {
		"name": "配送时间",
		"type": "DISTRIBUTION_DATE",
		"className": "easyui-datebox",
		"id": "input_DISTRIBUTION_DATE"
	}];
	var content = "";
	var s;
	for(var i = 0; i < data.length; i++) {
		s = searchType & (1 << i);
		if(s != 0) {
			content += "<td>" + data[i].name + "</td><td><input class=\"" + data[i].className + "\" name=\"" + data[i].type + "\" id=\"" + data[i].id + "\"/></td>";
		}
	}
	if(newdata.length > 0) {
		for(var i = 0; i < newdata.length; i++) {
			content += "<td>" + newdata[i].name + "</td><td><input class=\"" + newdata[i].className + "\" name=\"" + newdata[i].type + "\" id=\"" + newdata[i].id + "\"/></td>";
		}
	}
	var contain = "<div id=\"p\" class=\"easyui-panel\" title=\"查询\" style=\"width:100%;height:100%;\"><table cellspacing=\"4\" style=\"margin-top: 10px;\"><tr>" + content;
	contain += "<td><a href=\"#\" class=\"easyui-linkbutton\" onclick=\"doSearch()\" data-options=\"iconCls:'icon-search'\" style=\"width:80px;\">查询</a></td></tr></table></div>";
	return contain;
}

function initSearchData() {
	if($("#input_INTERNAL_NAME") != undefined) {
		$('#input_INTERNAL_NAME').combobox({
			valueField: 'INTERNAL_NAME',
			textField: 'INTERNAL_NAME',
			panelWidth: 200,
			panelHeight: 'auto',
			mode: 'remote',
			method: 'get',
			onChange: function(newValue, oldValue) {
				if(!isEnglish(newValue)) {
					return;
				}
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/logistics/internaltrade/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$('#input_INTERNAL_NAME').combobox("reload", url);
				}
			},
		});
	}

	if($("#input_CUSTOMER_TYPE") != undefined) {
		$('#input_CUSTOMER_TYPE').combobox({
			valueField: 'CUSTOMER_TYPE',
			textField: 'CUSTOMER_TYPE',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			editable:false,
			url: RemoteLinkAddress + "/logistics/customertype/listall.do",
		});
	}
	if($("#input_CUSTOMER_NAME") != undefined) {
		$('#input_CUSTOMER_NAME').combobox({
			valueField: 'CUSTOMER_NAME',
			textField: 'CUSTOMER_NAME',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			url: RemoteLinkAddress + '/logistics/customer/listall.do',
			onChange: function(newValue, oldValue) {
				if(newValue != "" && newValue != undefined) {
					var url = RemoteLinkAddress + "/logistics/customer/findbypinyin?PINYINFIELDNAME=" + newValue.trim();
					$(this).combobox("reload", url);
				}
			}
		});
	}
	if($("#input_PROVIDERTYPE") != undefined) {
		$('#input_PROVIDERTYPE').combobox({
			valueField: 'PROVIDERTYPE',
			textField: 'PROVIDERTYPE',
			panelHeight: 200,
			mode: 'remote',
			method: 'get',
			editable:false,
			url: RemoteLinkAddress + '/logistics/providertype/listall.do',
		});
	}
}

function processSearchCondition() {
	if($("#div_search") == undefined) {
		console.log("请检查ID为div_search是否存在");
		return "";
	}
	var searchCondition = "";
	$("#div_search input[name]").each(function(index) {
		if($(this).val().trim().length != 0) {
			if(searchCondition == "") {
				searchCondition = $(this).attr("name") + "=" + $(this).val().trim();
			} else {
				searchCondition += "&" + $(this).attr("name") + "=" + $(this).val().trim();
			}
		}
	});
	return searchCondition;
}